package com.chuanyang.driverh.mainmy.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.chuanyang.driverh.R;
import com.chuanyang.driverh.utils.widget.MyViewPager;

import java.util.ArrayList;


public class SubscribeSubscribedFragment extends Fragment implements RadioGroup.OnCheckedChangeListener{


    private View contentView;
    private MyViewPager vp_subscribe;
    private SubscribeFragment subscribeFragment;
    private SubscribedFragment subscribedFragment;
    private ArrayList list;
    private FragmentPagerAdapter adapter;

    RadioGroup rg_common;
    RadioButton rb_left;
    RadioButton rb_right;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        contentView = inflater.inflate(R.layout.common_frag_tab_two,null);
        initView();
        initData();
        initListener();
        return contentView;
    }

    private void initView() {
        rg_common=(RadioGroup)contentView.findViewById(R.id.rg_common);
        rb_left=(RadioButton)contentView.findViewById(R.id.rb_left);
        rb_right=(RadioButton)contentView.findViewById(R.id.rb_right);
        rb_left.setText("我订阅的");
        rb_right.setText("订阅我的");

    }

    public void initData() {
        vp_subscribe = (MyViewPager) contentView.findViewById(R.id.vp_common);
        list = new ArrayList();
        subscribeFragment = new SubscribeFragment();
        subscribedFragment = new SubscribedFragment();
        list.add(subscribeFragment);
        list.add(subscribedFragment);
        adapter = new FragmentPagerAdapter(getChildFragmentManager());
        vp_subscribe.setAdapter(adapter);
    }




    private void initListener() {
        rg_common.setOnCheckedChangeListener(this);
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId){
            case R.id.rb_left:
                vp_subscribe.setCurrentItem(0);
                subscribeFragment.onRefresh();
                break;
            case R.id.rb_right:
                vp_subscribe.setCurrentItem(1);
                subscribedFragment.onRefresh();
                break;
        }
    }


    class FragmentPagerAdapter extends android.support.v4.app.FragmentPagerAdapter {
        public FragmentPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Fragment getItem(int position) {
            return (Fragment) list.get(position);
        }

        @Override
        public int getItemPosition(Object object) {
            return super.getItemPosition(object);
        }
    }


}
